#include "res.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
Mix_Chunk *gameMusic;
int gMusicCondition = 1;
SDL_Texture *playerText;
TTF_Font *font;


void switchMusic()
{
    gMusicCondition = !gMusicCondition;
}

void playMusic()
{
    if (gMusicCondition)
    {
        if (Mix_Paused(1))
        {
            Mix_Resume(1);
        }
        else
        {
            Mix_PlayChannel(1, gameMusic, -1);
        }
    }
}
Mix_Chunk *loadWAV(const char *filepath)
{
    SDL_RWops *fileData = SDL_RWFromFile(filepath, "rb");
    if (!fileData)
    {
        SDL_Log("SDL_RWFromFile: %s\n", SDL_GetError());
        return NULL;
    }

    Mix_Chunk *chunk = Mix_LoadWAV_RW(fileData, 1);
    if (!chunk)
    {
        SDL_Log("Mix_LoadWAV_RW: %s\n", Mix_GetError());
        SDL_FreeRW(fileData);
        return NULL;
    }

    return chunk;
}
SDL_Texture *loadTexture(SDL_Renderer *render, char *path)
{
    SDL_Texture *newTexture = NULL;
    SDL_Surface *loadedSurface = IMG_Load(path);
    if (loadedSurface == NULL)
    {
        SDL_Log("Unable to load image %s! SDL_image Error: %s\n", path,
                IMG_GetError());
    }
    else
    {
        newTexture = SDL_CreateTextureFromSurface(render, loadedSurface);
        if (newTexture == NULL)
        {
            SDL_Log("Unable to create texture from %s! SDL Error: %s\n", path,
                    SDL_GetError());
        }
        SDL_FreeSurface(loadedSurface);
    }

    return newTexture;
}
int resLoad(SDL_Renderer *renderer)
{
     int imgFlags = IMG_INIT_PNG;
        if (!(IMG_Init(imgFlags) & imgFlags))
        {
          SDL_Log("SDL_image could not initialize! SDL_image Error: %s\n",
                 IMG_GetError());
          return 1;
        }
    Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096);
    Mix_AllocateChannels(16);
    gameMusic = loadWAV("bgm.ogg");
    if (gameMusic == NULL)
    {
        SDL_Log("Can not load texture, %s", IMG_GetError());
        return 1;
    }
    playerText = loadTexture(renderer,"player.png");
    
  if (TTF_Init() == -1)
  {
    SDL_Log("TTF unable to initialize! Error: %s\n", TTF_GetError());
  }
      font = TTF_OpenFont("fzpix.ttf", 20);
    if (playerText == NULL)
    {
        SDL_Log("Can not load texture, %s", IMG_GetError());
        return 1;
    }
    return 0;
}
void setTextTexture(SDL_Renderer *render, TEXT_TEXTURE *txtText, TTF_Font *font, char *text)
{
    SDL_Color textColor = {255, 0, 0, 255};
  SDL_Surface *textSurface = TTF_RenderUTF8_Blended(font, text, textColor);
  SDL_DestroyTexture(txtText->txtText);
  txtText->txtText = SDL_CreateTextureFromSurface(render, textSurface);
  txtText->txtDstRect = (SDL_Rect){0, 0, textSurface->w, textSurface->h};
  
  // Get rid of the obsolete surface
  SDL_FreeSurface(textSurface);
}

SDL_Texture *resGetPlayerText()
{
    return playerText;
}
TTF_Font *resGetFont(){
    return font;
}
void resUnLoad()
{
    SDL_DestroyTexture(playerText);
    TTF_Quit();
}
